//
//  YMLoginSegementView.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMLoginSegementView.h"


@interface YMLoginSegementView ()

@property (nonatomic, strong) UIButton *btnLeft;
@property (nonatomic, strong) UIButton *btnRight;

@end

@implementation YMLoginSegementView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = STheme.D9;
    self.layer.borderColor = [SUIUtil colorOf:0xE5E7EB].CGColor;
    self.layer.borderWidth = [SFitUtil fit:1];
    self.layer.cornerRadius = [SFitUtil fit:25];
    [self addSubview:self.btnLeft];
    [self addSubview:self.btnRight];
    
    [self.btnLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:4]);
        make.height.mas_equalTo([SFitUtil fit:42]);
    }];
    [self.btnRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo([SFitUtil fit:-4]);
        make.height.width.mas_equalTo(self.btnLeft);
        make.left.equalTo(self.btnLeft.mas_right).offset([SFitUtil fit:4]);
    }];
    
}

- (void)setIndexNow:(NSInteger)indexNow{
    _indexNow = indexNow;
    
    if (_indexNow == 0){
        self.btnLeft.selected = YES;
        self.btnRight.selected = NO;
        self.btnLeft.titleLabel.font = [SFitUtil fitBoldFont:15];
        self.btnRight.titleLabel.font = [SFitUtil fitFont:15];
    }else{
        self.btnLeft.selected = NO;
        self.btnRight.selected = YES;
        self.btnRight.titleLabel.font = [SFitUtil fitBoldFont:15];
        self.btnLeft.titleLabel.font = [SFitUtil fitFont:15];
    }
}

- (void)segementClickAction:(UIButton *)sender{
    self.indexNow = sender.tag-200;
    if (self.segementClickBlock){
        self.segementClickBlock(sender.tag-200);
    }
}

#pragma mark -- setter and getter
- (UIButton *)btnLeft{
    if (_btnLeft == nil){
        _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnLeft setImage:[UIImage imageNamed:@"login_icon_duanxin_nor"] forState:UIControlStateNormal];
        [_btnLeft setImage:[UIImage imageNamed:@"login_icon_duanxin_sel"] forState:UIControlStateSelected];
        [_btnLeft setImage:[UIImage imageNamed:@"login_icon_password_sel"] forState:UIControlStateHighlighted|UIControlStateSelected];
        [_btnLeft setTitle:KLanguage(@"sms_code_login_txt") forState:UIControlStateNormal];
        [_btnLeft setTitleColor:STheme.D4 forState:UIControlStateNormal];
        [_btnLeft setTitleColor:STheme.D12 forState:UIControlStateSelected];
        _btnLeft.selected = YES;
        [_btnLeft addTarget:self action:@selector(segementClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnLeft.tag = 200;
        _btnRight.titleLabel.font = [SFitUtil fitBoldFont:15];
        _btnLeft.layer.cornerRadius = [SFitUtil fit:21];
        _btnLeft.clipsToBounds = YES;
        [_btnLeft setBackgroundImage:[UIImage imageWithColor:STheme.D14] forState:UIControlStateSelected];
        [_btnLeft setBackgroundImage:[UIImage imageWithColor:STheme.D9] forState:UIControlStateNormal];
        [_btnLeft setImageEdgeInsets:UIEdgeInsetsMake(0, [SFitUtil fit:-3], 0, 0)];
        [_btnLeft setTitleEdgeInsets:UIEdgeInsetsMake(0, [SFitUtil fit:3], 0, 0)];
        _btnLeft.highlighted = NO;
        _btnLeft.titleLabel.adjustsFontSizeToFitWidth = YES;
        _btnLeft.titleLabel.minimumScaleFactor = 0.6;
    }
    return _btnLeft;
}
- (UIButton *)btnRight{
    if (_btnRight == nil){
        _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnRight setImage:[UIImage imageNamed:@"login_icon_password_nor"] forState:UIControlStateNormal];
        [_btnRight setImage:[UIImage imageNamed:@"login_icon_password_sel"] forState:UIControlStateSelected];
        [_btnRight setImage:[UIImage imageNamed:@"login_icon_password_sel"] forState:UIControlStateHighlighted|UIControlStateSelected];
        [_btnRight setTitle:KLanguage(@"account_pwd_login_txt") forState:UIControlStateNormal];
        [_btnRight setTitleColor:STheme.D4 forState:UIControlStateNormal];
        [_btnRight setTitleColor:STheme.D12 forState:UIControlStateSelected];
        [_btnRight addTarget:self action:@selector(segementClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnRight.tag = 201;
        _btnRight.titleLabel.font = [SFitUtil fitFont:15];
        _btnRight.layer.cornerRadius = [SFitUtil fit:21];
        _btnRight.clipsToBounds = YES;
        [_btnRight setBackgroundImage:[UIImage imageWithColor:STheme.D14] forState:UIControlStateSelected];
        [_btnRight setBackgroundImage:[UIImage imageWithColor:STheme.D9] forState:UIControlStateNormal];
        [_btnRight setImageEdgeInsets:UIEdgeInsetsMake(0, [SFitUtil fit:-3], 0, 0)];
        [_btnRight setTitleEdgeInsets:UIEdgeInsetsMake(0, [SFitUtil fit:3], 0, 0)];
        _btnRight.titleLabel.adjustsFontSizeToFitWidth = YES;
        _btnRight.titleLabel.minimumScaleFactor = 0.6;
    }
    return _btnRight;
}

@end



